package com.ysx.admin.service.impl;

import com.ysx.admin.pojo.AdChannel;
import com.ysx.admin.service.AdWemediaNewsAutoScanService;
import com.ysx.article.dto.ApArticleDto;
import com.ysx.article.feign.ApArticleDtoFeign;
import com.ysx.article.feign.ApAuthorFeign;
import com.ysx.article.pojo.ApArticle;
import com.ysx.article.pojo.ApArticleConfig;
import com.ysx.article.pojo.ApArticleContent;
import com.ysx.article.pojo.ApAuthor;
import com.ysx.common.utils.RequestHeaderInfoUtils;
import com.ysx.wemedia.feign.WeMediaNewsFeign;
import com.ysx.wemedia.pojo.WmNews;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自媒体的服务
 */
@Component
public class AdWemediaNewsAutoScanServiceImpl implements AdWemediaNewsAutoScanService {
    @Autowired
    private ApArticleDtoFeign apArticleDtoFeign;//feign 文章dto

    @Autowired
    private WeMediaNewsFeign weMediaNewsFeign;//feign 微服务查询自媒体素材

    @Autowired
    private ApAuthorFeign apAuthorFeign;//feign 微服务查询作者表

    @Autowired
    private AdChannelServiceImpl adChannelServiceImpl;//频道列表

    //将自媒体素材发布到文章
    @Override
    public void AdPublishNewsToArticleService(WmNews wmNews) {
        String userId = RequestHeaderInfoUtils.getHeaderUserId();

        //创建ArticleDto对象
        ApArticleDto apArticleDto = new ApArticleDto();
        //文章对象
        ApArticle apArticle = new ApArticle();
        apArticle.setTitle(wmNews.getTitle());//设置标题
        //获取作者信息
        ApAuthor apAuthor = apAuthorFeign.selectByUserId(Integer.parseInt(userId));
        if (null != apAuthor) {
            apArticle.setAuthorId(apAuthor.getId());
            apArticle.setAuthorName(apAuthor.getName());
        }
        //获取文章信息
        AdChannel adChannel = adChannelServiceImpl.getById(wmNews.getChannelId());
        if (null != adChannel) {
            apArticle.setChannelId(adChannel.getId());
            apArticle.setChannelName(adChannel.getName());
        }
        //文章布局
        apArticle.setLayout(wmNews.getType());
        //普通文章
        apArticle.setFlag(0);
        BeanUtils.copyProperties(wmNews,apArticle);
        //同步状态
        apArticle.setSyncStatus(wmNews.getStatus());
        apArticle.setCreatedTime(LocalDateTime.now());
        apArticleDto.setApArticle(apArticle);// 1/3封装

        //文章配置对象
        ApArticleConfig apArticleConfig = new ApArticleConfig();
        apArticleConfig.setIsDown(0);//是否下架
        apArticleConfig.setIsDelete(0);//是否删除
        apArticleDto.setApArticleConfig(apArticleConfig);// 2/3封装

        //文章内容对象
        ApArticleContent apArticleContent = new ApArticleContent();
        apArticleContent.setContent(wmNews.getContent());
        apArticleDto.setApArticleContent(apArticleContent);// 3/3封装

        //调用feign接口 传入对象 保存三个值 返回文章id
        ApArticle apArticleFromFeign = apArticleDtoFeign.saveDto(apArticleDto).getData();
        //将文章id更新到自媒体中
        wmNews.setArticleId(apArticleFromFeign.getId());
        weMediaNewsFeign.updateByPrimaryKey(wmNews);

/*
        //保存数据到ES中 // TODO 如果审核通过，则需要将数据存储到ES中
        ArticleInfoDocument articleInfoDocument = JSON.parseObject(JSON.toJSONString(data), ArticleInfoDocument.class);
        try {
            articleDocumentSearchFeign.saveToEs(articleInfoDocument);
        } catch (Exception e) {
            e.printStackTrace();
        }*/

    }
}
